package com.sky.demo.service.impl;

import java.util.List;
import java.util.Map;

import org.realogic.btp.sequence.KSequenceGenerator;
import org.realogic.cfm.domain.KPageResult;
import org.realogic.cfm.persistence.query.QueryCondition;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageInfo;
import com.sky.demo.entity.EntityBill;
import com.sky.demo.entity.EntityItem;
import com.sky.demo.repository.EntityBillDao;
import com.sky.demo.repository.EntityItemDao;
import com.sky.demo.service.EntityBillService;

/**
 * @see EntityBillService
 */
@Service("entityBillService")
public class EntityBillServiceImpl implements EntityBillService {

	// private Log log = LogFactory.getLog(getClass());

	private EntityBillDao entityBillDao;

	private EntityItemDao entityItemDao;

	public EntityBillServiceImpl() {

	}

	public EntityBillDao getEntityBillDao() {
		return entityBillDao;
	}

	@Autowired
	public void setEntityBillDao(EntityBillDao entityBillDao) {
		this.entityBillDao = entityBillDao;
	}

	public EntityItemDao getEntityItemDao() {
		return entityItemDao;
	}

	@Autowired
	public void setEntityItemDao(EntityItemDao entityItemDao) {
		this.entityItemDao = entityItemDao;
	}

	// @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public int create(final EntityBill aModel) {
		aModel.setId(KSequenceGenerator.uuidOfLang());
		return entityBillDao.create(aModel);
	}

	// @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public int update(final EntityBill aModel) {
		return entityBillDao.update(aModel);
	}

	// @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public int remove(final Long aKey) {
		// 删除子[实体]中的数据
		entityItemDao.deleteByMasterPK(aKey);
		// 删除主[实体]数据
		return entityBillDao.remove(aKey);
	}

	public EntityBill read(final Long aKey) {
		return entityBillDao.read(aKey);
	}

	public KPageResult query(QueryCondition aCondition) {
		List<EntityBill> tmpList = entityBillDao.query(aCondition);
		KPageResult temp = null;
		if (tmpList != null) {
			temp = new KPageResult(tmpList, aCondition.getTotalSize(), aCondition.getPageNo(), aCondition.getPageSize());
		}
		return temp;
	}

	public PageInfo<EntityBill> query(Map<String, Object> aCondition, int aPageNo, int aPageSize) {
		return entityBillDao.query(aCondition, aPageNo, aPageSize);
	}

	public EntityBill readEntity(final Long aKey) {
		EntityBill tmpModel = entityBillDao.read(aKey);
		List<EntityItem> tmpList = entityItemDao.loadByMasterPK(aKey);
		tmpModel.setItems(tmpList);
		return tmpModel;
	}

	// @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public int createEntity(final EntityBill aModel) {
		aModel.setId(KSequenceGenerator.uuidOfLang());
		List<EntityItem> tmpItems = aModel.getItems();
		if (tmpItems != null) {
			int lineNO = 1;
			for (EntityItem tmpItem : tmpItems) {
				// 设置子[实体]Key
				tmpItem.setId(KSequenceGenerator.uuidOfLang());
				// 设置关联主表的属性值
				tmpItem.setEntityBillID(aModel.getId());
				tmpItem.setLinevalue(lineNO);
				lineNO++;
			}
		}
		// 创建主[实体]数据
		int createdCount = entityBillDao.create(aModel);
		if (tmpItems != null) {
			// 创建子[实体]数据
			entityItemDao.create(tmpItems);
		}
		return createdCount;
	}

	// @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public int updateEntity(final EntityBill aModel) {
		return 1;
	}

}